package org.niugang.config;



import org.niugang.exception.AuthExceptionEntryPoint;
import org.niugang.exception.CustomAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
/**
 * 
 * Copyright: Copyright (c) 2018 863263957@qq.com
 * 
 * @ClassName: ResourceServerConfigurer.java
 * @Description:资源服务器安全配置 
 * @author: niugang
 * @date: 2018年9月14日 下午3:42:11
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {

	@Autowired
	private CustomAccessDeniedHandler customAccessDeniedHandler;

	
	@Override
	public void configure(ResourceServerSecurityConfigurer resources) {

		resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
		resources.accessDeniedHandler(customAccessDeniedHandler);
		
	}
	
    @Override
    public void configure(HttpSecurity http) throws Exception {
      
        http.authorizeRequests()
                .antMatchers(HttpMethod.POST,"/userservice/user").permitAll()  //创建用户请求时允许的，其他都需要认证
                .anyRequest().authenticated();
    }

}
